package cn.tedu.oa_system.service.impl;

import cn.tedu.oa_system.base.json_result.ResultDataDto;
import cn.tedu.oa_system.base.json_result.ResultDto;
import cn.tedu.oa_system.mapper.StatusListMapper;
import cn.tedu.oa_system.pojo.StatusList;
import cn.tedu.oa_system.pojo.in.FindLikeKeyIn;
import cn.tedu.oa_system.pojo.in.StatusListIn;
import cn.tedu.oa_system.service.StatusListService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;

import static cn.tedu.oa_system.base.constant.DataBaseConstants.DATABASE_AFFECTED_ROW;

/**
 * @Author MW
 * @Description 状态服务实现
 * @CreateDate 2023/3/8
 */
@Service
@Slf4j
@Transactional
public class StatusListServiceImpl implements StatusListService {

    @Autowired
    private StatusListMapper statusListMapper;

    @Override
    public ResultDto<Void> addNew(StatusListIn statusListIn) {
        LambdaQueryWrapper<StatusList> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(StatusList::getStatusName,statusListIn.getStatusName())
                .eq(StatusList::getStatusModel,statusListIn.getStatusModel());
        Integer integer = statusListMapper.selectCount(wrapper);
        Assert.isTrue(integer==0,"该状态已存在");
        StatusList statusList=new StatusList();
        BeanUtils.copyProperties(statusListIn,statusList);
        int row = statusListMapper.insert(statusList);
        Assert.isTrue(row==DATABASE_AFFECTED_ROW,"添加状态失败");
        ResultDto<Void> resultDto=new ResultDto<>();
        resultDto.setIsSuccess(true);
        return resultDto;
    }

    @Override
    public ResultDto<Void> delete(StatusListIn statusListIn) {
        int row = statusListMapper.deleteById(statusListIn.getStatusId());
        Assert.isTrue(row==DATABASE_AFFECTED_ROW,"删除状态失败");
        ResultDto<Void> resultDto=new ResultDto<>();
        resultDto.setIsSuccess(true);
        return resultDto;
    }

    @Override
    public ResultDto<Void> update(StatusListIn statusListIn) {
        StatusList statusList=new StatusList();
        BeanUtils.copyProperties(statusListIn,statusList);
        int row = statusListMapper.updateById(statusList);
        Assert.isTrue(row==DATABASE_AFFECTED_ROW,"修改状态失败");
        ResultDto<Void> resultDto=new ResultDto<>();
        resultDto.setIsSuccess(true);
        return resultDto;
    }

    @Override
    public ResultDataDto<StatusList> list(FindLikeKeyIn likeKeyIn) {
        //先判断是否为模糊查询
        LambdaQueryWrapper<StatusList> wrapper=new LambdaQueryWrapper<>();
        ResultDataDto<StatusList> resultDataDto=new ResultDataDto<>();
        if (likeKeyIn.getLikeKey()==null){
            //不为null则为查询全部状态列表
            wrapper.ne(StatusList::getStatusId,-1);
            List<StatusList> statusLists = statusListMapper.selectList(wrapper);
            resultDataDto.setDataList(statusLists);
            resultDataDto.setIsSuccess(true);
            return resultDataDto;
        }
        //若likeKeyIn不为null,则为模糊查询
        wrapper.like(StatusList::getStatusModel,likeKeyIn.getLikeKey())
                .like(StatusList::getStatusName,likeKeyIn.getLikeKey());
        List<StatusList> statusLists = statusListMapper.selectList(wrapper);
        resultDataDto.setDataList(statusLists);
        resultDataDto.setIsSuccess(true);
        return resultDataDto;
    }
}
